package com.sh.ssao.lottery.choujiang.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sh.ssao.lottery.config.result.Result;
import com.sh.ssao.lottery.choujiang.service.IBachInfoService;
import com.sh.ssao.lottery.config.poi.ExcelUtil;
import com.sh.ssao.lottery.config.utils.StrUtil;
import com.sh.ssao.lottery.choujiang.bean.BachInfo;
import com.sh.ssao.lottery.config.utils.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RestController;

/********************************
 * 积跬步，以至千里；积小流，以成江海 *
 * ******************************
 * <p>
 * 抽奖批次 前端控制器
 * </p>
 * bach_info
 * @author gzgs
 * @since 2022-12-08 15:27:40
 */
@Slf4j
@RestController
@RequestMapping("/choujiang/bach_info")
public class BachInfoController {


    @Autowired
    IBachInfoService service;

    @Autowired
    Result result;
    /**
     * 抽奖批次 - 添加页面跳转
     *
     * @param model
     * @return
     */
    @RequestMapping(value = "/add_page" )
    public ModelAndView add_page(Model model, @RequestParam(name = "menuid", required = false, defaultValue = "" ) String menuid){
        model.addAttribute("menuid",menuid);
        return new ModelAndView("/busi/bach_info/add" );
    }

    /**
     * 抽奖批次 - 数据添加
     *
     * @param bean
     * @param request
     * @return
     */
    @RequestMapping(value = "/add" )
    public Result add(BachInfo bean,HttpServletRequest request){
        try{
        log.debug("新增--->>>>" );
        //session 获取数据
        HttpSession session=request.getSession();
        List<Map<String, Object>>roles=(List<Map<String, Object>>)session.getAttribute("roles" );

        String tid= StrUtil.generatorId("");

        service.saveMore(bean);

        return result.ok(bean);
        }catch(Exception e){
        e.printStackTrace();
        return result.fail(bean);
        }
    }

    /**
     * 抽奖批次 - 跳转修改页面
     *
     * @param bean
     * @param model
     * @return
     */
    @RequestMapping(value = "/edit_page" )
    public ModelAndView edit_page(BachInfo bean,Model model,
                                  @RequestParam(name = "menuid", required = false, defaultValue = "" ) String menuid){
        try{
        log.debug("新增--->>>>" );
        bean=service.getById(bean.getBachId());
          //返回数据
        model.addAttribute("record",bean);
        model.addAttribute("menuid",menuid);
        return new ModelAndView("/busi/bach_info/edit" );
        }catch(Exception e){
        e.printStackTrace();
        return null;
        }
    }

    /**
     * 抽奖批次 - 数据修改
     *
     * @param bean
     * @param model
     * @param request
     * @return
     */
    @RequestMapping(value = "/edit" )
    public Result edit(BachInfo bean,Model model,HttpServletRequest request){
        try{
        log.debug("修改--->>>>" );

        service.updateMore(bean);

        return result.ok(bean);
        }catch(Exception e){
        e.printStackTrace();
        return result.fail(bean);
        }
    }

    /**
     * 抽奖批次 - 数据删除
     *
     * @param bean
     * @return
     */
    @RequestMapping(value = "/del" )
    public Result del(BachInfo bean){
        try{
        log.debug("删除--->>>>" );
        service.delMore(bean);
        return result.ok(bean);
        }catch(Exception e){
        e.printStackTrace();
        return result.fail(bean);
        }
    }

    /**
     * 抽奖批次 - 列表
     *
     * @param bean
     * @param model
     * @return
     */
    @RequestMapping(value = "/list" )
    public ModelAndView listPage(BachInfo bean,Model model,
                                @RequestParam(name = "menuid", required = false, defaultValue = "" ) String menuid){
        try{
        log.debug("列表查询--->>>>" );
        model.addAttribute("menuid",menuid);
            model.addAttribute("rd",bean);
            return new ModelAndView("/busi/bach_info/list");
        }catch(Exception e){
            e.printStackTrace();
            return new ModelAndView("/busi/bach_info/list");
        }
    }

    /**
     * 抽奖批次 - 异步分页
     *
     * @param bean
     * @param model
     * @param current
     * @param size
     * @return
     */
    @RequestMapping(value = "/list_json" )
    @ResponseBody
    public Map<String, Object> listJson(BachInfo bean,Model model,
    @RequestParam(name = "current", required = false, defaultValue = "1" ) long current,
    @RequestParam(name = "size", required = false, defaultValue = "10" ) long size){
        Map<String, Object> map=new HashMap();
        try{
            Page page=new Page(current,size);
            QueryWrapper<BachInfo> queryWrapper=new QueryWrapper<>();
            if(null!=bean&&null!=bean.getBachId()&&!"".equals(String.valueOf(bean.getBachId()))){
                queryWrapper.eq("bach_id",bean.getBachId());
            }             if(null!=bean&&null!=bean.getBackNo()&&!"".equals(String.valueOf(bean.getBackNo()))){
                queryWrapper.eq("back_no",bean.getBackNo());
            } 
        //查询数据
        IPage<BachInfo> page1 = service.listPage(page, queryWrapper);
        map.put("msg","ok" );
        map.put("data",page1.getRecords());
        map.put("code",0);
        map.put("count",page.getTotal());
        return map;
        }catch(Exception e){
        e.printStackTrace();
        return map;
        }
    }

    /**
     * 抽奖批次 - 导出报表
     *
     * @param bean
     * @return
     */
    @RequestMapping(value = "/export")
    @ResponseBody
    public void export(BachInfo bean, HttpServletResponse response) throws IOException {
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
        QueryWrapper<BachInfo> queryWrapper = new QueryWrapper<>();
        List<Map<String, Object>> maps = service.listMaps(queryWrapper);

        String[] headers ={"id","批次号",""};
        String[] fields = {"bach_id","back_no",""};
        String name = "抽奖批次信息";
        String title= "抽奖批次信息"+ StrUtil.simpleDate("yyyy-MM-dd HH:mm:ss");

        Workbook hwb = new ExcelUtil().exportExcelXLSX(name, title,headers,fields,maps,StrUtil.simpleDate("yyyy-MM-dd HH:mm:ss"));

        response.reset();
        response.setContentType("multipart/form-data");
        response.addHeader("Content-Disposition","attachment; filename=\"" + new String(title.getBytes("GBK"), "ISO8859_1")+".xlsx\"");
        OutputStream outs = response.getOutputStream();
        hwb.write(outs);
        outs.close();

        } catch (Exception e) {
        e.printStackTrace();

        }finally {

        }
    }

}
